import { _decorator, Component, Node, UITransform, tween, sp, easing, EventTouch, Vec2, View, Vec3, CCInteger } from 'cc';
import { BaseObject } from '../../ccc3-framework-core/framework/modules/baseObject';
import { SpineComponent } from '../../ccc3-framework-core/utils/SpineComponent';
const { ccclass, property } = _decorator;

@ccclass('partControl')
export class partControl extends BaseObject {
    @property(Node)
    mapNode: Node = null;

    //初始时的列数位置，左右切换时，不更改此值，
    //更改在container中的位置，mapController是按子节点从上到下的顺序读取
    @property(CCInteger)
    posIdx: number = 0;

    startIndex: number = 0;

    start() {
        // this.bindTouchStart(this.node, this.onTouchStart)
        // this.bindTouchMove(this.node, this.onTouchMove)
        // this.bindTouchEnd(this.node, this.onTouchEnd)
    }
    private isMoving: boolean = false;
    private _partIndex: number = 0;
    public get partIndex() {
        return this._partIndex;
    };
    private set partIndex(value: number) {
        this._partIndex = value;
    }
    public setStartIndex(index) {
        //取index后一位
        index = index.length > 1 ? index.substr(1) : index;
        this.startIndex = parseInt(index);
        this.partIndex = this.startIndex;
        this.setToMap(this.partIndex);
    }
    public onEnable() {
        this.stopSpines();
    }
    stopSpines() {
        this.mapNode.children.forEach(element => {
            let sp = element.getChildByName("sp");
            let spineCmp = sp.getComponent(SpineComponent);
            spineCmp.pause();
        })
    }

    public startPos: Vec2 = null;
    public onTouchStart(e: EventTouch) {
        this.startPos = e.getUILocation();
    }
    private onTouchMove(e: EventTouch) {

    }
    public onTouchEnd(e: EventTouch) {
        if (this.isMoving) return;
        let endPos = e.getUILocation();
        let dis = endPos.subtract(this.startPos);
        if (Math.abs(dis.y) > 50) {
            this.moveToMap(dis.y > 0, this.partIndex);
        }
    }
    private moveToMap(moveUp: boolean, nowIndex: number) {
        this.isMoving = true;
        let nextIndex = 0;
        let fromNode = this.mapNode.children[nowIndex];
        let toNode = null;
        let tweenDuration: number = 1.0;
        let moveY = View.instance.getDesignResolutionSize().height;
        if (moveUp) {
            nextIndex = nowIndex + 1;
            if (nextIndex > this.mapNode.children.length - 1) {
                nextIndex = 0;
            }
            toNode = this.mapNode.children[nextIndex];

            tween(fromNode)
                .to(tweenDuration, { position: new Vec3(0, moveY, 0) }).start();

            toNode.setPosition(0, -moveY);
            tween(toNode)
                .to(tweenDuration, { position: new Vec3(0, 0, 0) }).call(() => {
                    this.partIndex = nextIndex;
                    this.isMoving = false;
                }).start();
        }
        else {
            nextIndex = nowIndex - 1;
            if (nextIndex < 0) {
                nextIndex = this.mapNode.children.length - 1
            }
            toNode = this.mapNode.children[nextIndex];
            toNode.setPosition(0, moveY);



            tween(fromNode)
                .to(tweenDuration, { position: new Vec3(0, -moveY, 0) }).start();


            tween(toNode)
                .to(tweenDuration, { position: new Vec3(0, 0, 0) }).call(() => {
                    this.partIndex = nextIndex;
                    this.isMoving = false;
                }).start();
        }

    }

    public setToMap(index: number) {
        if (index > this.mapNode.children.length - 1) {
            index = 0;
        }
        this.mapNode.children.forEach((element, idx) => {
            if (idx == index) {
                this.mapNode.children[idx].setPosition(0, 0);
            }
            else {
                this.mapNode.children[idx].setPosition(0, -View.instance.getDesignResolutionSize().height);
            }
        });
    }

    update(deltaTime: number) {

    }
}

